package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.ProjectType;
import com.dingreading.cloud.admin.service.ProjectTypeService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/projectType")
@Api(tags = "总部-收支项目")
public class ProjectTypeController {

    @Resource
    private ProjectTypeService projectTypeService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "订单号/手机号") @RequestParam(value = "keyword", required = false) String keyword
    ) {
        Page<ProjectType> page = this.projectTypeService.pageList(pageUtil, keyword);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "获取所有数据")
    @PostMapping(value = "/getList", produces = "application/json; charset=UTF-8")
    public R<Object> getList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "项目名") @RequestParam(value = "name", required = false) String name
    ) {
        List<ProjectType> data = projectTypeService.getList(name);

        return R.ok(data);
    }


    @ApiOperation(value = "获取详情")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        ProjectType data = projectTypeService.getById(id);
        if (data == null) return R.fail("获取失败");

        return R.ok(data);
    }


    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete", produces = "application/json; charset=UTF-8")
    public R<Object> delete(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        ProjectType data = projectTypeService.getById(id);
        if (data == null) return R.fail("获取失败");
        if (data.getSource().equals(1)) return R.fail("系统数据，不能删除");

        projectTypeService.removeById(id);

        return R.ok();
    }


    @ApiOperation(value = "新增/编辑")
    @PostMapping(value = "/edit", produces = "application/json; charset=UTF-8")
    public R<Object> edit(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "商品id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "商品名") @RequestParam(value = "name", required = false) String name
    ) {
        if (id == null || id <= 0) {
            long cnt = projectTypeService.getSameNameCnt(null, name);
            if (cnt > 0) return R.fail("项目名已存在");
            // 新增
            ProjectType data = newProjectType(new ProjectType(), name);
            projectTypeService.saveOrUpdate(data);
        } else {
            // 修改
            long cnt = projectTypeService.getSameNameCnt(id, name);
            if (cnt > 0) return R.fail("项目名已存在");

            ProjectType data = projectTypeService.getById(id);
            if (data == null) return R.fail("获取失败");
            if (data.getSource().equals(1)) return R.fail("系统数据，不能修改");

            newProjectType(data, name);
            projectTypeService.saveOrUpdate(data);
        }
        return R.ok();
    }

    /**
     * 新建收支项目
     */
    public static ProjectType newProjectType(ProjectType data, String name) {
        if (data.getId() == null || data.getId() <= 0) {
            data.setSource(2);
        }

        if (name != null)
            data.setName(name);
        return data;
    }

}
